package xhwl.hmo.mall.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.api.FeedBackApi;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.NumberJudgeUtils;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

public class FeedBackActivity extends BaseActivity implements OnClickListener {

    EditText et_feedback_content, et_feedback_phone;
    private Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
    }

    public Boolean judgeNull() {
        String msg = "";
        if (TextUtils.isEmpty(et_feedback_content.getText())) {
            msg = "请输入反馈信息";
        }
//        else if (TextUtils.isEmpty(et_feedback_phone.getText())) {
//            msg = "请输入您的联系方式";
//        }
//        else if (false == NumberJudgeUtils.isMobileNO(et_feedback_phone.getText().toString())) {
//            msg = "手机号格式有误，请输入正确的手机号";
//        }
        if (TextUtils.isEmpty(msg)) {
            return true;
        } else {
            Toast.makeText(FeedBackActivity.this, msg, Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.submit:
                if (judgeNull()) {
//                    FeedBackApi api = new FeedBackApi(et_feedback_content.getText().toString(), et_feedback_phone.getText().toString());
                    FeedBackApi api = new FeedBackApi(et_feedback_content.getText().toString(), SpUtil.getString(SpUtil.MEMBER_MOBILE,""));
                    HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp>() {
                        @Override
                        public void onNext(HttpResp resp) {
                            if (null != resp) {
                                if (resp.isOk()) {
                                    Toast.makeText(FeedBackActivity.this, "提交反馈成功", Toast.LENGTH_LONG).show();
                                    FeedBackActivity.this.finish();
                                } else {
                                    Toast.makeText(FeedBackActivity.this, "提交反馈失败" + resp.message, Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void initViews() {
        initTitleViews();
        submit = (Button) findViewById(R.id.submit);
        et_feedback_content = (EditText) findViewById(R.id.et_feedback_content);
        et_feedback_phone = (EditText) findViewById(R.id.et_feedback_phone);
        et_feedback_phone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    boolean bool = NumberJudgeUtils.isMobileNO(et_feedback_phone.getText().toString());
                    if (bool == false) {
                        ToastUtil.showToast(mContext, "手机号格式有误，请重新输入");
                        et_feedback_phone.setText("");
                    }
                }

            }
        });
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("意见反馈");
    }

    @Override
    public void initListener() {
        submit.setOnClickListener(this);
    }

    @Override
    public void loadData() {
        // TODO Auto-generated method stub

    }
}
